////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek, 1999-2011.
//
// -------------------------------------------------------------------------
//  File name:   FlowMusicEndThemeNode.h
//  Version:     v1.00
//  Created:     08/04/2011 by Thomas Wollenzin
//  Description: This is the implementation of the FlowMusicEndThemeNode class
//
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "FlowBaseNode.h"

class CFlowNode_MusicEndTheme : public CFlowBaseNode<eNCT_Instanced>
{

public:

	//////////////////////////////////////////////////////////////////////////
	CFlowNode_MusicEndTheme(SActivationInfo* pActInfo) 
	{
	}

	//////////////////////////////////////////////////////////////////////////
	~CFlowNode_MusicEndTheme()
	{
	}

	//////////////////////////////////////////////////////////////////////////
	IFlowNodePtr Clone(SActivationInfo* pActInfo)
	{
		return new CFlowNode_MusicEndTheme(pActInfo);
	}

	enum INPUTS 
	{
		eIN_STOP,
		eIN_STOP_MODE,
		eIN_DELAY_END_LAYER,
		eIN_END_EVERYTHING,

		eIN_NUM_PORTS
	};

	enum OUTPUTS 
	{
	};

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void GetConfiguration(SFlowNodeConfig& config)
	{
		static const SInputPortConfig inputs[] = 
		{
			InputPortConfig_Void("Stop", _HELP("Stops the currently playing theme from being processed."), "Stop"),
			InputPortConfig<int>("StopMode", EThemeFade_FadeOut, _HELP("FadeOut: Music gradually fades out.\nStopAtOnce: Music stops immediately.\nPlayEndLayer: Music stops by immediately playing the EndLayer pattern of this Mood.\nPlayEndLayerAtFadePoint: Music stops by playing the EndLayer pattern of this Mood but waits for a sync point of the currently playing pattern."), 0, _UICONFIG("enum_int:StopAtOnce=0,FadeOut=1,PlayEndLayer=2, PlayEndLayerAtFadePoint=3")),
			InputPortConfig<int>("ForceEndLimit", _HELP("When \"StopMode\" is set to \"PlayEndLayer\" this indicates the least amount of seconds to the end of the currently playing pattern in which a switch is allowed but when \"StopMode\" is set to \"PlayEndLayerAtFadePoint\" this value indicates the amount of time in seconds to the next fade point of the currently playing pattern where a layer switch is not allowed anymore."), "ForceEndLimit"),
			InputPortConfig<bool>("EndEverything", _HELP("Indicates whether to stop every type of pattern that's currently playing or just the theme."), "EndEverything"),
			InputPortConfig_Null()
		};

		static const SOutputPortConfig outputs[] = 
		{
			OutputPortConfig_Null()
		};

		config.pInputPorts  = inputs;
		config.pOutputPorts = outputs;
		config.sDescription = _HELP("End Theme");
		config.SetCategory(EFLN_APPROVED);
	}

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void Serialize(SActivationInfo* pActivationInfo, TSerialize ser)
	{
	}

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void ProcessEvent(EFlowEvent event, SActivationInfo* pActInfo)
	{
		switch (event)
		{
		case eFE_Initialize:
			{
			}
			break;
		case eFE_Activate:
			{
				string const sCurrentMood(gEnv->pMusicSystem->GetMood());

				if (IsPortActive(pActInfo, eIN_STOP))
				{
					bool const bEndEverything           = GetPortBool(pActInfo, eIN_END_EVERYTHING);
					EThemeFadeType const eThemeFadeType = (EThemeFadeType)GetPortInt(pActInfo, eIN_STOP_MODE);
					int const nForceEndLimitInSec       = GetPortInt(pActInfo, eIN_DELAY_END_LAYER);

					switch (eThemeFadeType)
					{
					case EThemeFade_StopAtOnce:
					case EThemeFade_FadeOut:
					case EThemeFade_PlayEnd:
					case EThemeFade_PlayEndAtFadePoint:
						{
							gEnv->pMusicSystem->EndTheme(eThemeFadeType, nForceEndLimitInSec, bEndEverything);
						}
						break;
					}
				}
			}
			break;
		}
	}

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void GetMemoryUsage(ICrySizer * s) const
	{
		s->Add(*this);
	}
};

REGISTER_FLOW_NODE("Music:EndTheme", CFlowNode_MusicEndTheme);
